﻿using System;
using System.ComponentModel;
namespace SilverlightHVP.ViewModel
{
    public class PlayerViewModel : INotifyPropertyChanged
    {

        public event PropertyChangedEventHandler PropertyChanged;
        private TimeSpan previousPosition = new TimeSpan(0, 0, 0);
        private readonly TimeSpan Interval = new TimeSpan(0, 0, 5);
        private readonly TimeSpan StartPosition = new TimeSpan(0, 0, 0);

        public PlayerViewModel(State state)
        {
            this.state = state;
            state.CurrentItemChanged += new State.CurrentItemChangedHandler(CurrentItemOrSetChanged);
            state.CurrentTopicChanged += new State.CurrentTopicChangedHandler(state_CurrentTopicChanged);
            SetURI();
        }

        void state_CurrentTopicChanged(object sender, EventArgs e)
        {
            OnPropertyChanged("Offset");
        }

        private State state;
        private Uri theUri;
        public Uri URI
        {
            get 
            {
               return theUri; 
            }
            set
            {
                theUri = value;
                OnPropertyChanged("URI");
            }
        }

       /*   HACK!! We are binding the getter of Offset to the Position of the CoreSmoothStreamingMediaElement
            but we are binding the setter to the PositionOverride property of the (temporary) class HVPCoreMediaElement that
            derives from CoreSmoothStreamingMediaElement.  Also note that this is two way binding and we need both the getter
            and the setter. 
            To Fix: 
               1. Combine move get and set from PlayerPosition to Offset and remove PlayerPosition
               2. Change Binding in SmoothStreamingPlayer to bind to Offset, two way
               3. Remove file (and class) HVPCoreMediaElement.cs
       */

        public TimeSpan Offset {  get { return state.Position; } }

        public TimeSpan PlayerPosition
        {
           get { return state.Position; }

           set
           {
              if ( value > previousPosition + Interval )
              {
                 previousPosition = value;
                 state.Position = value;
              }
           }
        }

        // this will be the command called through mvvm
        public void VideoEnded()
        {
           state.SetNextItem();
        }

        private void CurrentItemOrSetChanged(object sender, EventArgs e)
        {
            previousPosition = StartPosition;
            state.Position = StartPosition;
            SetURI();
        }

        private void SetURI()
        {
           if ( state.CurrentItem == null )
              return;

           URI = state.CurrentItem.ItemUri;
           OnPropertyChanged( "URI" );
        }

        private void OnPropertyChanged(string propName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
    }
}
